const mongoose = require('mongoose')

// 1.连接
mongoose.connect('mongodb://localhost:27017/mongo_test', { 
  useNewUrlParser: true // 避免创建索引警告
})

const conn = mongoose.connection
conn.on('error', () => console.error('连接数据库失败'))
conn.once('open', async () => {
  // 2.定义一个Schema - Table
  const Schema = mongoose.Schema({
    category: String,
    name: String,
  })

  // 3.编译一个Model, 它对应数据库中复数、小写的Collection
  const Model = mongoose.model('mongoosefruit', Schema)
  try {
    // 4.创建，create返回Promise
    let r = await Model.create({
      category: '温带水果',
      name: '香蕉55',
      price: 5,
    })
    console.log('插入数据:', r)

    const blogSchema = mongoose.Schema({
      // 定义校验规则
      title: { type: String, required: [true, '标题为必填项'] }, 
      author: String,
      body: String,
      // 定义对象数组
      comments: [{ body: String, date: Date }], 
      // 指定默认值
      date: { type: Date, default: Date.now }, 
      hidden: Boolean,
      meta: {
        // 定义对象
        votes: Number,
        favs: Number,
      },
    })

    // 定义实例方法
    blogSchema.methods.findByAuthor = function (author) {
      return this.model('blog').find({ author: this.author }).exec()
    }

    // 静态方法
    blogSchema.statics.findByAuthor = function (author) {
      return this.model('blog').find({ author }).exec()
    }

    // 虚拟属性
    blogSchema.virtual('commentsCount').get(function () {
      return this.comments.length
    })

    // 获得模型实例
    const BlogModel = mongoose.model('blog', blogSchema)
    await BlogModel.deleteMany({})
    const blog = new BlogModel({
      title: 'nodejs持久化',
      author: 'jerry',
      body: '....',
      comments: [{ body: 'haha' }],
    })
    r = await blog.save()

    // 调⽤实例⽅法
    r = await blog.findByAuthor()
    console.log('findByAuthor', r)

    // 静态方法
    r = await BlogModel.findByAuthor('jerry')
    console.log('findByAuthor', r)

    // 虚拟属性
    r = await BlogModel.findOne({ author: 'jerry' })
    console.log('blog留言数:', r.commentsCount)
  } catch (e) {
    console.log('e', e.message)
  }
})
